<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notification extends CI_Controller {
    
    var $Array_Data;
    
    public function get_notification(){
        $UserInfo = $this->session->userdata('UserInfo');
        $Notification_Info = array();
        $offset = 0;
        if($UserInfo['UserType'] == 'work'){
            $CI =& get_instance();
            $CI->load->model('Users3_Notification_model','Us3Noti');
            $Notification_Info = $CI->Us3Noti->Get_Advanced("*",
                    array('RelUserEmail'    =>  $UserInfo['UserEmail']),
                    'AppearTime desc',
                    10,0
                    );
            //Arrange Data
            $temp = array();
            foreach($Notification_Info as $Noti){
                if($Noti['Type'] = 'Accept'){
                    $CI->load->model('Posts_model','Posts');
                    $Post = $CI->Posts->Get(array('PostID'  => $Noti['RelPostID']));
                    $Post = $Post[0];
                    $Noti['PictureURL'] = $Post['PictureURL'];
                    $Noti['Content']    = str_replace("#POSTNAME#", $Post['Subject'], $Noti['Content']);
                    $temp[] = $Noti;
                }
                else{
                    $temp[] = $Noti;
                }
            }
            $Notification_Info = $temp;
            //Arrange Data for View
            $this->Array_Data['UserInfo'] = $UserInfo;
            $this->Array_Data['Notification_Info']= $Notification_Info;
            $this->load->view('user_group3/notification_view',$this->Array_Data);

        }else{
            $this->load->view('index.html');
        }
    }
    
    public function num_new_not(){
        $UserInfo = $this->session->userdata('UserInfo');
        if($UserInfo['UserType'] == 'work'){
            $CI =& get_instance();
            $CI->load->model('Users3_Notification_model','Us3Noti');
            $NumNot = $CI->Us3Noti->Get(array(
                'IsReaded'          => 0,
                'RelUserEmail'      => $UserInfo['UserEmail']));
            echo count($NumNot);
        }
    }
    
    public function show_notification($ID){
        $AdditionalData = $_GET['Additional'];
        $CI =& get_instance();
        $CI->load->model('Users3_Notification_model', 'Noti');
        $Noti =  $CI->Noti->Get(array('NotiID' => $ID));
        $Noti = $Noti[0];
        if($Noti['Type'] == 'Accept')
        { 
            $CI->Noti->Update(array('IsReaded' =>  1),array('NotiID' => $ID));
            redirect(site_url().'/user_group3/jobs/'.$AdditionalData[0]);
        }    
    }
}